import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class SwingPanelThread extends JFrame implements Runnable {
    public static String game_title = "猜拳游戏";//设置游戏标题
    public static String authorInfo = "";//游戏作者信息
    public static String img_jiandao = "1.png";
    public static String img_shitou = "2.png";
    public static String img_bu = "3.png";

    int x; // 图片循环
    int n = 0; // 获胜次数
    boolean yes;
    JLabel jieguo = new JLabel("你已经获胜0次", JLabel.CENTER);
    JLabel bian = new JLabel();
    JLabel my = new JLabel();
    JLabel xinxi = new JLabel("", JLabel.CENTER); // 结果提示
    JButton bu = new JButton("开始!");

    public SwingPanelThread() {
        //**************** 绘制主体游戏面板
        setTitle(game_title);
        setSize(400, 650);
        setLocationRelativeTo(null);
        Container con = getContentPane();
        con.setBackground(Color.orange);
        con.setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //**************** 绘制主体游戏面板

        //**************** 设置游戏面板中，最下方的展示内容
        JLabel name = new JLabel(authorInfo);       //个人信息
        name.setFont(new Font("微软雅黑", Font.LAYOUT_LEFT_TO_RIGHT, 20));
        name.setBounds(45, 565, 300, 30);
        con.add(name);
        //**************** 设置游戏面板中，最下方的展示内容

        //**************** 游戏结果展示
        jieguo.setFont(new Font("宋体", Font.BOLD, 16));     //成绩
        jieguo.setBounds(75, 15, 232, 22);
        bian.setBounds(35, 55, 90, 90);
        bian.setIcon(new ImageIcon(Caiquan.class.getResource("/" + img_jiandao)));
        my.setBounds(255, 55, 90, 90);
        my.setIcon(new ImageIcon(Caiquan.class.getResource("/one.png")));
        xinxi.setForeground(Color.RED);
        xinxi.setBounds(135, 55, 120, 90);
        xinxi.setFont(new Font("微软雅黑", Font.LAYOUT_LEFT_TO_RIGHT, 16));
        con.add(bian);
        con.add(jieguo);
        con.add(my);
        con.add(xinxi);
        JPanel pan = new JPanel(null);
        pan.setBackground(new Color(255, 255, 255));
        pan.setBounds(15, 200, 350, 350);
        con.add(pan);
        JLabel boy = new JLabel("出拳吧，骚年", JLabel.CENTER);
        boy.setFont(new Font("宋体", Font.BOLD, 16));
        boy.setBounds(105, 15, 145, 25);
        pan.add(boy);
        JPanel pan2 = new JPanel(new GridLayout(1, 3, 20, 0));
        pan2.setBounds(25, 55, 300, 90);
        pan2.setBackground(new Color(255, 255, 255));
        bu.setFont(new Font("微软雅黑", 666, 22));
        bu.setBackground(new Color(250, 236, 56));
        bu.setBounds(39, 275, 275, 45);
        pan.add(bu);
        pan.add(pan2);
        JButton jd = new JButton();
        JButton st = new JButton();
        JButton b = new JButton();
        jd.setIcon(new ImageIcon(Caiquan.class.getResource("/" + img_jiandao)));
        st.setIcon(new ImageIcon(Caiquan.class.getResource("/" + img_shitou)));
        b.setIcon(new ImageIcon(Caiquan.class.getResource("/" + img_bu)));
        pan2.add(jd);
        pan2.add(st);
        pan2.add(b);

        // 开始按钮
        bu.addActionListener(e -> {
            yes = true;
            my.setIcon(new ImageIcon(Caiquan.class.getResource("/one.png")));
            xinxi.setText("");
        });
        // 剪刀
        jd.addActionListener(e -> {
            if (yes == true) {
                yes = false;
                my.setIcon(new ImageIcon(Caiquan.class.getResource("/" + img_jiandao)));
                bu.setText("再来！");
                if (x == 1) {
                    xinxi.setText("打平手了！");
                } else if (x == 2) {
                    xinxi.setText("你输了！");
                } else if (x == 3) {
                    xinxi.setText("你赢了！");
                    n++;
                    jieguo.setText("你已经获胜" + n + "次");
                }
            }
        });
        // 石头
        st.addActionListener(e -> {
            if (yes == true) {
                yes = false;
                my.setIcon(new ImageIcon(Caiquan.class.getResource("/" + img_shitou)));
                bu.setText("再来！");
                if (x == 2) {
                    xinxi.setText("打平手了！");
                } else if (x == 3) {
                    xinxi.setText("你输了！");
                } else if (x == 1) {
                    xinxi.setText("你赢了！");
                    n++;
                    jieguo.setText("你已经获胜" + n + "次");
                }
            }
        });
        // 布
        b.addActionListener(e -> {
            if (yes == true) {
                yes = false;
                my.setIcon(new ImageIcon(Caiquan.class.getResource("/" + img_bu)));
                bu.setText("再来！");
                if (x == 3) {
                    xinxi.setText("打平手了！");
                } else if (x == 1) {
                    xinxi.setText("你输了！");
                } else if (x == 2) {
                    xinxi.setText("你赢了！");
                    n++;
                    jieguo.setText("你已经获胜" + n + "次");
                }
            }
        });
        setVisible(true);
    }

    public void run() {
        while (true) {
            while (yes) {
                x = new Random().nextInt(3) + 1;
                bian.setIcon(new ImageIcon(Caiquan.class.getResource("/" + x + ".png")));
            }
            try {
                Thread.sleep(125);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
